package demos.modifysql;

import gudusoft.gsqlparser.EDbVendor;
import gudusoft.gsqlparser.TGSqlParser;
import gudusoft.gsqlparser.TStatementList;
import gudusoft.gsqlparser.stmt.TSelectSqlStatement;

/**
 * @author chen
 * @date 2023/8/1 16:11
 */
public class replaceTablenameTwo {
    public static void main(String[] args) {
        TGSqlParser sqlparser = new TGSqlParser(EDbVendor.dbvmysql);
        sqlparser.sqltext = "-- mysql\nSELECT `user`.name,\n       `order`.price\nFROM   `user`,\n       `order`\nWHERE  `user`.id = `order`.uid";
        int ret = sqlparser.parse();
        if (ret == 0) {
            TStatementList stmts = sqlparser.getSqlstatements();
            TSelectSqlStatement stmt = (TSelectSqlStatement) stmts.get(0);

            // getResultColumn 获取 select 语句形成的 record set 中的 column
            stmt.getResultColumnList().forEach(column -> {
                System.out.printf("%s(%s) -> %s(RS-1)\n",
                        column.getDisplayName(),
                        column.getPrefixTable(),
                        column.getDisplayName()
                );
            });
        } else {
            System.out.println(sqlparser.getErrormessage());
        }
    }
}
